package org.mspring.mlog.web.module.web.forums;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.bbs.Forum;
import org.mspring.mlog.entity.bbs.Topic;
import org.mspring.mlog.service.bbs.ForumService;
import org.mspring.mlog.service.bbs.TopicService;
import org.mspring.mlog.web.query.bbs.TopicQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author Gao Youbo
 * @since 2013年8月9日
 */
@Widget
@RequestMapping("/forums")
public class BBS_ForumWidget extends BBS_AbstractWidget {
    @Autowired
    private ForumService forumService;
    @Autowired
    private TopicService topicService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/{id}")
    public String forums(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer page = RequestUtils.getRequestParameterAsInteger(request, "page");
        if (page == null) {
            page = 1;
        }
        Page<Topic> topicPage = new Page<Topic>();
        topicPage.setPageNo(page);

        Forum forum = forumService.get(id);

        Map queryParams = new HashMap();
        queryParams.put("forum.id", id);
        topicPage = topicService.listPage(new TopicQueryCriterion(queryParams), topicPage);

        model.addAttribute("forum", forum);
        model.addAttribute("topicPage", topicPage);
        return "skin:/forums/forum";
    }

}
